import requests
from bs4 import BeautifulSoup
import json
import time

def forward_message(target_webhook_url, message):
    # 发送请求到目标 Webhook URL 并传递消息
    headers = {
            "Content-Type": "application/json; charset=utf-8",
        }
    payload_message = {
            "msg_type": "text",
            "content": {
                "text": message 
            }
        }
    requests.post(target_webhook_url,  data=json.dumps(payload_message),headers=headers)

def listen_and_forward(feishu_webhook_url, target_webhook_url):
    # 每隔一段时间监听一次飞书 Webhook URL
    while True:
        # 发送请求到飞书 Webhook URL 并获取响应
        headers = {
            "Content-Type": "application/json; charset=utf-8",
        }
        payload_message = {
            "msg_type": "text",
           
        }
        response = requests.post(feishu_webhook_url,data=json.dumps(payload_message),headers=headers)
        if response.status_code == 200:
            # 使用 BeautifulSoup 解析响应的 HTML 内容
            print(response.text)
            soup = BeautifulSoup(response.text, 'html.parser')
            # 在解析后的 HTML 中找到需要的数据，这里假设需要找到一个元素的文本内容
            message = "lll"
            
            # 将解析得到的数据发送到目标 Webhook URL
            forward_message(target_webhook_url, message)
        else:
            print(f'请求失败，状态码：{response.status_code}')
        
        # 每隔一段时间再监听一次
        time.sleep(60)

# 飞书 Webhook URL
feishu_webhook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/164ee6b8-93f7-480e-81d2-f86601af1458'

# 目标 Webhook URL
target_webhook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/aa75d6fd-023a-4b79-b89f-8ca953b20dec'

# 开始监听并转发消息
listen_and_forward(feishu_webhook_url, target_webhook_url)